<?php

namespace app\supplier\controller;

use think\Controller;
use app\erp\model\Uploaddb;
use app\erp\model\SiteConfigdb;

class Upload extends Base
{
    public function uploadReName()
    {
        $param = request()->param();
        $files = request()->file();
        // 获取最后的序列号

        $ins = json_decode($param['params'], true);
        $last_id = $param['caigou_invoice_count'];
        // 获取对应的id
        $ins_arr = [];
        foreach ($ins as $key => $val) {
            $temp = explode('-', $key);
            $ins_arr[$val] = $temp[1];
        }
        foreach ($files as $file) {
            $get_file = $file->getInfo();
            $file_ins = $ins_arr[$get_file['name']] + 1;
            $ext = explode('.', $get_file['name']);
            $file_name = $param['out_order_no'] . '采购发票(序号：' . ($last_id + $file_ins) . ')';
            $file_path = $file_name . '.' . end($ext);
            $info = $file->move('./uploads/', date('Ymd') . '/' . $file_path);
            if ($info) {
                $get_info = $info->getInfo();
                $param ['size'] = $get_info ['size'];
                $param ['type'] = $get_info ['type'];
                $param ['save_path'] = 'https://tailongji.dixintong.com/uploads/' . $info->getSaveName();
                $param ['file_name'] = $file_name;
                $uploaddb = new Uploaddb ();
                $uploaddb->param = $param;
                $info = $uploaddb->ajaxSave();
                $this->layuiReturn($info, 0, 'success');
            } else {
                $error = $info->getError();
                $this->layuiReturn($error, 0, $error, -1);
            }
        }
    }

    public function uploadReName_back()
    {
        $get_file_name = request()->param();
        $files = request()->file();
        foreach ($files as $file) {
            $get_file = $file->getInfo();
            $ext = explode('.', $get_file['name']);
            $uploaddb = new Uploaddb ();
            $get_file_name ['file_name'] = $get_file_name ['file_name'] . '.' . end($ext);
            $info = $file->move('./uploads/', date('Ymd') . '/' . $get_file_name['file_name']);
            if ($info) {
                $get_info = $info->getInfo();
                $param ['size'] = $get_info ['size'];
                $param ['type'] = $get_info ['type'];
                $param ['save_path'] = 'https://tailongji.dixintong.com/uploads/' . $info->getSaveName();
                $param ['file_name'] = $get_file_name ['file_name'];
                $uploaddb->param = $param;
                $info = $uploaddb->ajaxSave();
                $this->layuiReturn($info, 0, 'success');
            } else {
                $error = $info->getError();
                $this->layuiReturn($error, 0, $error, -1);
            }
        }
    }

    public function uploadReName2()
    {
        $get_file_name = request()->param();
        $files = request()->file();
        foreach ($files as $file) {
            $get_file = $file->getInfo();
            $ext = explode('.', $get_file['name']);
            $uploaddb = new Uploaddb ();
            $get_file_name ['file_name'] = $get_file_name ['file_name'];
            $info = $file->move('./uploads/Excel/hetong/', $get_file_name['file_name']);
            if ($info) {
                $get_info = $info->getInfo();
                $param ['size'] = $get_info ['size'];
                $param ['type'] = $get_info ['type'];
                $param ['save_path'] = $_SERVER ['REQUEST_SCHEME'].'://'.$_SERVER ['HTTP_HOST'].'/uploads/Excel/hetong/' . $info->getSaveName();
                $param ['file_name'] = $get_file_name ['file_name'];
                $uploaddb->param = $param;
                $info = $uploaddb->ajaxSave();
                $this->layuiReturn($info, 0, 'success');
            } else {
                $error = $info->getError();
                $this->layuiReturn($error, 0, $error, -1);
            }
        }
    }

    public function uploadOne()
    {
        $files = request()->file();
        foreach ($files as $file) {
            $get_file = $file->getInfo();
            $param['size'] = $get_file['size'];
            $param['type'] = $get_file['type'];
            $param['file_name'] = $get_file['name'];
            $uploaddb = new Uploaddb();
            $uploaddb->param = $param;
            $chk_info = $uploaddb->ajaxFind();
            /*
            if ($chk_info) {
                // 如果已经上传过文件
                $this->thumb('./uploads/' . $chk_info['file_path']);
                $this->layuiReturn($chk_info, 0, 'success');
            } else {
            */
            // 开始上传
            $info = $file->move('./uploads/');
            if ($info) {
                $get_info = $info->getInfo();
                $param['size'] = $get_info['size'];
                $param['type'] = $get_info['type'];
                $param['save_path'] = $_SERVER['REQUEST_SCHEME'].'://'.$_SERVER ['HTTP_HOST'].'/uploads/' . $info->getSaveName();
                $param['file_name'] = time() . $get_file['name'];
                $uploaddb->param = $param;
                $info = $uploaddb->ajaxSave();
                $this->water('./uploads/' . $param['save_path']);
                $this->thumb('./uploads/' . $param['save_path']);
                $this->layuiReturn($info, 0, 'success');
            } else {
                $error = $info->getError();
                $this->layuiReturn($error, 0, $error, -1);
            }
            //}
        }
    }

    protected function water($src)
    {
        $siteConfigdb = new SiteConfigdb();
        $siteConfigdb->param['config_name'] = 'ISWATER';
        $get_iswater = $siteConfigdb->getValue();
        if ($get_iswater == '是') {
            $siteConfigdb->param['config_name'] = 'WATERURL';
            $get_waterurl = $siteConfigdb->getValue();
            $siteConfigdb->param['config_name'] = 'WATERALPHA';
            $get_wateralpha = $siteConfigdb->getValue();
            $get_wateralpha = $get_wateralpha ? $get_wateralpha : 1;
            $siteConfigdb->param['config_name'] = 'WATERPOSITION';
            $get_waterposition = $siteConfigdb->getValue();
            $waterposition = 1;
            switch ($get_waterposition) {
                case '左上':
                    $waterposition = 1;
                    break;
                case '正上':
                    $waterposition = 2;
                    break;
                case '右上':
                    $waterposition = 3;
                    break;
                case '左中':
                    $waterposition = 4;
                    break;
                case '正中':
                    $waterposition = 5;
                    break;
                case '右中':
                    $waterposition = 6;
                    break;
                case '左下':
                    $waterposition = 7;
                    break;
                case '正下':
                    $waterposition = 8;
                    break;
                case '右下':
                    $waterposition = 9;
                    break;
            }
            if ($get_waterurl) {
                $ext = pathinfo($src, PATHINFO_EXTENSION);
                $name = basename($src, '.' . $ext);
                $dir = dirname($src);
                if (in_array($ext, array(
                    'gif',
                    'jpg',
                    'jpeg',
                    'bmp',
                    'png'
                ))) {
                    $image = \think\Image::open($src);
                    $image->water($get_waterurl, $waterposition, $get_wateralpha);
                    $image->save($src);
                }
            }
        }
    }

    protected function thumb($src)
    {
        if (strstr($src, '_thumb') != false) {
            if (@fopen($src, 'r')) {
                return false;
            }
            $src = str_replace('_thumb', '', $src);
        }
        $siteConfigdb = new SiteConfigdb();
        $siteConfigdb->param['config_name'] = 'ISTHUMB';
        $get_isthumb = $siteConfigdb->getValue();
        if ($get_isthumb == '是') {
            $siteConfigdb->param['config_name'] = 'THUMBWIDTH';
            $width = $siteConfigdb->getValue();
            $siteConfigdb->param['config_name'] = 'THUMBHEIGHT';
            $height = $siteConfigdb->getValue();
            $siteConfigdb->param['config_name'] = 'THUMBTYPE';
            $type = $siteConfigdb->getValue();
        }

        if ($width == '') {
            $width = 220;
        }
        if ($height == '') {
            $height = 220;
        }
        $thumb_type = 1;
        switch ($type) {
            case '缩略图等比例缩放':
                $thumb_type = 1;
                break;
            case '缩略图缩放后填充':
                $thumb_type = 2;
                break;
            case '缩略图居中裁剪':
                $thumb_type = 3;
                break;
            case '缩略图左上角裁剪':
                $thumb_type = 4;
                break;
            case '缩略图右下角裁剪':
                $thumb_type = 5;
                break;
            case '缩略图固定尺寸缩':
                $thumb_type = 6;
                break;
        }
        if (is_file($src) && file_exists($src)) {
            $ext = pathinfo($src, PATHINFO_EXTENSION);
            $name = basename($src, '.' . $ext);
            $dir = dirname($src);
            if (in_array($ext, array(
                'gif',
                'jpg',
                'jpeg',
                'bmp',
                'png'
            ))) {
                $name = $name . '_thumb.' . $ext;
                $file = $dir . '/' . $name;
                if (!file_exists($file)) {
                    $image = \think\Image::open($src);
                    $image->thumb($width, $height);
                    $image->save($file);
                }
            }
        }
    }
}